﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.RF.Repair;
using BLL.RF.ChangeLog;
using Model.RF.Repair;
using Model.RF.ChangeLog;

public partial class ProductManage_RFProductManage_Manage_RepairConfigManage_RepariConfigManage : System.Web.UI.Page
{
    RepairRoute rr;
    RepairConfig rc;
    private string Path { get { return ViewState["RepairPath"].ToString(); } set { ViewState["RepairPath"] = value; } }

    public ProductManage_RFProductManage_Manage_RepairConfigManage_RepariConfigManage()
    {
        rr = new RepairRoute();
        rc = new RepairConfig();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepairPath();
            BindRepairStep();
        }
    }

    private void BindRepairPath()
    {
        this.RadioButtonList1.DataSource = rr.RepairRoute_Get();
        this.RadioButtonList1.DataTextField = "RepairPath";
        this.RadioButtonList1.DataValueField = "RepairPath";
        this.RadioButtonList1.DataBind();
    }

    private void BindRepairStep()
    {
        this.CheckBoxList1.DataSource = FlowStep.list;
        this.CheckBoxList1.DataTextField = "StepText";
        this.CheckBoxList1.DataValueField = "StepValue";
        this.CheckBoxList1.DataBind();
    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Path = this.RadioButtonList1.SelectedValue;
        this.CheckBoxList1.ClearSelection();
        IList<RepairConfigInfo> list = rc.RepairConfig_GetbyPath(Path);
        foreach (RepairConfigInfo item in list)
        {
            this.CheckBoxList1.Items.FindByText(item.RepairStep).Selected = true;
        }
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        string oldStatus = rc.GetRepairStepbyPath(Path);
        rc.RepairConfig_RemovebyPath(Path);
        foreach (ListItem item in this.CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                RepairConfigInfo rci = new RepairConfigInfo() { ID = 0, RepairPath = Path, RepairStep = item.Text };
                rc.Insert(rci);
            }
        }
        RepairConfig_ChangeLog_Add(oldStatus);
        string str1 = "返修路径对应需返修工序设定成功！";
        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
    }

    private void RepairConfig_ChangeLog_Add(string oldStatus)
    {
        string newStatus = rc.GetRepairStepbyPath(Path);
        RepairConfig_ChangeLogInfo rccinfo = new RepairConfig_ChangeLogInfo()
        {
            ID = 0,
            RecordTime = DateTime.Now,
            UserID = User.Identity.Name,
            IPAddress = IPGet.getIp(),
            RepairPath = Path,
            OldStatus = oldStatus,
            NewStatus = newStatus,
        };
        RepairConfig_ChangeLog rcc = new RepairConfig_ChangeLog();
        rcc.Insert(rccinfo); 
    }
}
